package com.xunqi.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xunqi.product.entity.AttrAttrgroupRelationEntity;
import com.xunqi.product.entity.AttrEntity;
import com.xunqi.product.service.AttrAttrgroupRelationService;
import com.xunqi.product.service.AttrService;
import com.xunqi.product.service.CategoryService;
import com.xunqi.product.vo.AttrGroupRelationVo;
import com.xunqi.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xunqi.product.entity.AttrGroupEntity;
import com.xunqi.product.service.AttrGroupService;
import com.xunqi.common.utils.PageUtils;
import com.xunqi.common.utils.R;



/**
 * 属性分组
 *
 * @author hjy
 * @email hjy@gmail.com
 * @date 2020-06-16 11:57:38
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		List<Long> path =  categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * 属性关联删除
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVo>  vos){

        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 属性关联列表
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){

        List<AttrEntity> attrEntities =  attrService.getRelation(attrGroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 属性列表
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrGroupId") Long attrGroupId){

        PageUtils pageUtils =  attrService.getNoRelation(params,attrGroupId);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 属性关联保存
     */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrAttrgroupRelationEntity>  vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 属性列表
     */
    @GetMapping("/{catelogId}/withattr")
    public R getWithAttrs(@RequestParam Map<String, Object> params,
                            @PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getWithAttrsByCatelogId(catelogId);

        return R.ok().put("data", vos);
    }

}
